package com.eurlanda.spark.cassandra

import org.apache.spark.SparkConf
import org.apache.spark.sql.{SaveMode, SparkSession}

/**
  * Created by zhudebin on 2017/4/6.
  */
object DFWriteDemo {

  def main(args: Array[String]) {

    val conf = new SparkConf().setMaster("local[4]").setAppName("test cassandra")

//    val spark = new SparkContext(conf)

    val spark = SparkSession.builder().config(conf).getOrCreate()

    val df = spark
      .read
      .format("org.apache.spark.sql.cassandra")
      .options(Map( "table" -> "words",
        "keyspace" -> "test",
        "cluster" -> "mycluster",
        "cassandra.validate_type" -> "1",
        "engine.spark.cassandra.username" -> "cassandra",
        "engine.spark.cassandra.password" -> "cassandra",
        "engine.spark.cassandra.host" -> "192.168.137.104",
        "engine.spark.cassandra.port" -> "9042"))
      .load()

    val df2 = df.select("id")

    df2.write.format("org.apache.spark.sql.cassandra")
      .mode(saveMode = SaveMode.Append)
      .options(Map( "table" -> "words_bak",
      "keyspace" -> "test",
      "cluster" -> "mycluster",
      "cassandra.validate_type" -> "1",
      "engine.spark.cassandra.username" -> "cassandra",
      "engine.spark.cassandra.password" -> "cassandra",
      "engine.spark.cassandra.host" -> "192.168.137.104",
      "engine.spark.cassandra.port" -> "9042")).save()

    spark.close()
  }

}
